package window.frame;

import com.formdev.flatlaf.FlatIntelliJLaf;
import entity.TravelGuideInfo;
import window.tab.HomeTab;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import static manage.TravelGuideDBManager.updateLikeCount;

public class TravelGuideDetailFrame extends JFrame {

    private TravelGuideInfo travelGuideInfo;

    public TravelGuideDetailFrame(TravelGuideInfo travelGuideInfo, HomeTab homeTab) {
        this.travelGuideInfo = travelGuideInfo;
        setTitle(travelGuideInfo.getTitle());
        setSize(800, 600);
        setLocationRelativeTo(null);

        // 添加窗口关闭监听器
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if (travelGuideInfo.isLiked()) {
                    updateLikeCount(travelGuideInfo, travelGuideInfo.getLikeCount());
                    homeTab.loadTravelGuides();
                }
                super.windowClosing(e);
            }
        });

        // 设置主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());

        // 左侧图像部分
        JLabel imageLabel = new JLabel();
        ImageIcon originalImageIcon = new ImageIcon(travelGuideInfo.getImagePath());
        Image originalImage = originalImageIcon.getImage();
        Image scaledImage = originalImage.getScaledInstance(400, 500, Image.SCALE_SMOOTH);
        ImageIcon scaledImageIcon = new ImageIcon(scaledImage);
        imageLabel.setIcon(scaledImageIcon);
        mainPanel.add(imageLabel, BorderLayout.WEST);

        // 右侧文字部分
        JPanel textPanel = new JPanel();
        textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS));
        textPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JEditorPane textArea = new JEditorPane("text/html", "<html><h1>" + travelGuideInfo.getTitle() + "</h1>" + travelGuideInfo.getContent() + "</html>");
        textArea.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setPreferredSize(new Dimension(350, 500));

        // 确保滚动条在顶部
        SwingUtilities.invokeLater(() -> scrollPane.getViewport().setViewPosition(new Point(0, 0)));

        textPanel.add(scrollPane);
        mainPanel.add(textPanel, BorderLayout.CENTER);

        // 底部按钮部分
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        JLabel likeCountLabel = new JLabel("like: " + travelGuideInfo.getLikeCount());
        JButton likeButton = new JButton("like");

        likeButton.addActionListener(e -> {
            travelGuideInfo.toggleLike();
            likeCountLabel.setText("like: " + travelGuideInfo.getLikeCount());
        });

        buttonPanel.add(likeCountLabel);
        buttonPanel.add(likeButton);
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 5, 5));

        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(mainPanel);
    }

    public static void main(String[] args) throws UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(new FlatIntelliJLaf());
        TravelGuideInfo travelGuideInfo = new TravelGuideInfo(1, "A Journey to Hangzhou: Wandering West Lake, Savoring the Blend of Classic and Modern"
                , "<p>A Journey to Hangzhou: Wandering West Lake, Savoring the Blend of Classic and Modern</p><p>Stepping into Hangzhou feels like entering a world of poetry...</p>"
                , "res/TravelGuideImage/lake.jpg"
                , 100);
        TravelGuideDetailFrame frame = new TravelGuideDetailFrame(travelGuideInfo, new HomeTab());
        frame.setVisible(true);
    }
}
